import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chen.haogang
 * 
 */
public class MilkShakes {

	private static BigInteger totalLoops;
	private static long startTime;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(""
					.getClass().getResourceAsStream("/B-small-practice.in")));
			int testCaseNumber = Integer.valueOf(br.readLine());
			for (int i = 0; i < testCaseNumber; i++) {
				totalLoops = new BigInteger("1");
				customerFlavors = new ArrayList<List<Milkshake>>();
				flavorNumber = Integer.valueOf(br.readLine());
				int customers = Integer.valueOf(br.readLine());
				for (int j = 0; j < customers; j++) {
					String[] flavors = br.readLine().split(" ");
					totalLoops = totalLoops.multiply(new BigInteger(flavors[0]));
					List<Milkshake> temp = new ArrayList<Milkshake>();
					for (int k = 0; k < Integer.valueOf(flavors[0]); k++) {
						temp.add(new Milkshake(new Integer(flavors[2 * k + 1]), new Integer(flavors[2 * k + 2])));
					}
					Collections.sort(temp);
					if(!customerFlavors.contains(temp)) {
						customerFlavors.add(temp);
					}
				}
				List<Integer> result = new ArrayList<Integer>();
				for (int j = 0; j < flavorNumber; j++) {
					result.add(-1);
				}
				results = new ArrayList<List<Integer>>();
				startTime = System.currentTimeMillis();
				getPossibleFlavors(0, 0, result);
				System.out.println(results);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	private static int flavorNumber;
	/**
	 * all customers' favorite milkshakes flavor
	 */
	private static List<List<Milkshake>> customerFlavors;

	/**
	 * all possible results
	 */
	private static List<List<Integer>> results;

	private static void getPossibleFlavors(int customerIndex,
			int customerFlavorIndex, List<Integer> result) {
//		System.out.println("customer:" + customerIndex);
		for (int i = customerFlavorIndex; i < customerFlavors
				.get(customerIndex).size(); i++) {
//			System.out.println("customer flavor:" + i);
			int flavorIndex = customerFlavors.get(customerIndex).get(i).flavor - 1;
			int malted = customerFlavors.get(customerIndex).get(i).malted;
			if (result.get(flavorIndex) < 0
					|| result.get(flavorIndex).equals(malted)) {
				result.set(flavorIndex, malted);
				if (customerIndex + 1 < customerFlavors.size()) {
					getPossibleFlavors(customerIndex + 1, 0, result);
				} else {
					totalLoops = totalLoops.add(new BigInteger("-1"));
					if (System.currentTimeMillis() - startTime > 20000) {
						System.out.println(totalLoops);
						startTime = System.currentTimeMillis();
					}
					if (!results.contains(result)) {
						results.add(new ArrayList<Integer>(result));
						System.out.println(result);
					}
				}
				result.set(flavorIndex, -1);
			}
		}
	}
	
	static class Milkshake implements Comparable<Milkshake> {
		int flavor;
		int malted;
		
		public Milkshake(int flavor, int malted) {
			this.flavor = flavor;
			this.malted = malted;
		}
		
		public boolean equals(Object o) {
			return flavor == ((Milkshake)o).flavor;
		}
		
		@Override
		public int compareTo(Milkshake o) {
			if(flavor == o.flavor)
				return 0;
			else if (flavor < o.flavor)
				return -1;
			else
				return 1;
		}
		public String toString() {
			return flavor + "," + malted;
		}
	}
}
